package net.ib.mtalk.ui.buddy;

import java.util.ArrayList;

import net.ib.mtalk.R;
import net.ib.mtalk.base.BaseTabActivity;
import net.ib.mtalk.ui.HomeActivity;
import net.ib.mtalk.ui.buddy.contact.BuddyContactListActivity;
import net.ib.mtalk.ui.buddy.suggestion.BuddySuggestionListActivity;
import net.ib.mtalk.ui.buddy.talk.BuddyTalkListActivity;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

public class BuddyTabActivity extends TabActivity {

	private final int INDICATOR_MTALK_FRIENDS		= 1;
	private final int INDICATOR_CONTACT_FRIENDS		= 2;
	private final int INDICATOR_SUGGESTION_FRIENDS	= 3;
	
	private static final String TAG_MTALK_BUDDY			= "mtalk_buddy";
	private static final String TAG_CONTACT_BUDDY		= "contact_buddy";
	private static final String TAG_SUGGESTION_BUDDY 	= "suggestino_buddy";
	
	private static TextView mMnTalkBadge;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buddy_tab);
		
		final TabHost host = getTabHost();
		final Intent intentMtalkBuddy = new Intent(this, BuddyTalkListActivity.class);
		intentMtalkBuddy.setAction(Intent.ACTION_VIEW);
		intentMtalkBuddy.addCategory(Intent.CATEGORY_TAB);
		final Intent intentPhoneBuddy = new Intent(this, BuddyContactListActivity.class);
		intentPhoneBuddy.setAction(Intent.ACTION_VIEW);
		intentPhoneBuddy.addCategory(Intent.CATEGORY_TAB);
		final Intent intentSuggestionBuddy = new Intent(this, BuddySuggestionListActivity.class);
		intentSuggestionBuddy.setAction(Intent.ACTION_VIEW);
		intentSuggestionBuddy.addCategory(Intent.CATEGORY_TAB);
		
		host.addTab(host.newTabSpec(TAG_MTALK_BUDDY)
				.setIndicator(buildIndicator(INDICATOR_MTALK_FRIENDS))
				.setContent(intentMtalkBuddy));

		host.addTab(host.newTabSpec(TAG_CONTACT_BUDDY)
				.setIndicator(buildIndicator(INDICATOR_CONTACT_FRIENDS))
				.setContent(intentPhoneBuddy));
		host.addTab(host.newTabSpec(TAG_SUGGESTION_BUDDY)
				.setIndicator(buildIndicator(INDICATOR_SUGGESTION_FRIENDS))
				.setContent(intentSuggestionBuddy));
		
		getTabWidget().setStripEnabled(false);
	}
	
	public static void updateMnTalkBadge(int count)
	{
		if(count > 0)
		{
			mMnTalkBadge.setVisibility(View.VISIBLE);
			mMnTalkBadge.setText(String.valueOf(count));
		}
		else
			mMnTalkBadge.setVisibility(View.GONE);
		
		HomeActivity.selectNewBuddy();
	}
	
	private View buildIndicator(int tabKind) {
		final RelativeLayout indicator = (RelativeLayout) getLayoutInflater().inflate(R.layout.tab_indicator_buddy, getTabWidget(), false);
		TextView label = (TextView)indicator.findViewById(R.id.tab_label);
        if(indicator.isSelected()){
        	label.setTextColor(R.color.tab_selected_text);
        } else {
        	label.setTextColor(R.color.tab_not_selected_text);
        }
        switch (tabKind) {
		case INDICATOR_MTALK_FRIENDS:
			label.setText(getString(R.string.buddy_talk_tag));
			mMnTalkBadge = (TextView)indicator.findViewById(R.id.talk_badge);
			mMnTalkBadge.setVisibility(View.VISIBLE);
			break;
		case INDICATOR_CONTACT_FRIENDS:
			label.setText(getString(R.string.buddy_contact_tag));
			mMnTalkBadge.setVisibility(View.GONE);
			break;
		case INDICATOR_SUGGESTION_FRIENDS:
			label.setText(getString(R.string.buddy_suggestion_tag));
			mMnTalkBadge.setVisibility(View.GONE);
			break;
		default:
			break;
		}
        return indicator;
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case BuddyTalkListActivity.REQ_TALK_VOICE_RECOGNITION:
			ArrayList<String> matches_talk = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			BuddyTalkListActivity.searchBuddy.setText(BuddyTalkListActivity.searchBuddy.getText() + matches_talk.get(0));
			break;
		case BuddyContactListActivity.REQ_CONTACT_VOICE_RECOGNITION:
			ArrayList<String> matches_contact = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			BuddyContactListActivity.buddy_search.setText(BuddyContactListActivity.buddy_search.getText() + matches_contact.get(0));
			break;
		default:
			break;
		}
	}
}
